package cn.ddd.linktran.adapter.http;

import cn.ddd.linktran.adapter.constant.API;
import cn.ddd.linktran.client.api.UrlMapService;
import cn.ddd.linktran.client.dto.cmd.UrlMapAddCmd;
import cn.ddd.linktran.client.dto.data.UrlMapDTO;
import cn.ddd.linktran.component.ratelimiter.LimitType;
import cn.ddd.linktran.component.ratelimiter.RateLimiter;
import cn.ddd.linktran.domain.domainservice.token.TokenCheck;
import com.alibaba.cola.dto.SingleResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;


@RestController
@Slf4j
@RequestMapping(API.WEB_API_PATH)
public class UrlMapController {
    @Autowired
    private UrlMapService urlMapService;

    /**
     * 传入长链接，创建短链映射
     * @param exchange @RateLimiter 注解要求方法第一个参数是 ServerWebExchange
     * @param urlMapAddCmd
     * @return
     */
    @PostMapping("/create")
    @RateLimiter(count = 3, time = 5, limitType = LimitType.IP)
    @TokenCheck
    public SingleResponse<UrlMapDTO> createUrlMap(ServerWebExchange exchange,@RequestBody UrlMapAddCmd urlMapAddCmd){
        return urlMapService.createUrlMap(urlMapAddCmd);
    }
}
